--
-- 2024-8-26
--
local RxHttp = {
  requestTab = {
    tstab = 'tstab',
    header_send = { typeget = 'android'},
    body_send = nil,
    format = 'gbk'
  }
}

-- 请求结果 - 封装处理
function RxHttpResponDeal(res)
  local result = {
    status = false,
    data = nil,
    message = '是否发生，内部解析错误'
  }
  if res == false then
    result.message = '请求出错，请联系开发者'
    return result
  end

  RxLog('RxHttpResponDeal res='..tostring(res))
  if type(res) == 'string' and res then
    local r = RxHttp:tableJson(res)
    -- RxLog('r type ='..tostring(type(r)))
    if r and type(r) == 'table' then
      local status = r.status or false
      local message = r.message or r.msg or ''
      local data = r.data
      -- RxLog('RxHttpResponDeal first status='..tostring(status))
      -- RxLog('RxHttpResponDeal first message='..tostring(message))
      -- RxLog('RxHttpResponDeal first data='..tostring(data))
      result.status = status
      result.message = message
      result.data = data
      return result
    else
      result.data = r
    end

    RxLog('RxHttpResponDeal first 处理结果不是正确的返回值')
  end

  return result
end

-- Get - http
function RxHttp:Get(url, data, isDealJson)
  if not url then return nil end

  if isDealJson == nil then
    isDealJson = true
  end

  -- RxLog('RxHttp:Get 111 isDealJson='..tostring(isDealJson))

  local ts = require('ts')
  -- https://helpdoc.touchsprite.com/dev_docs/25/6.html
  -- 超时时间，单位为秒
  ts.setHttpsTimeOut(60)
  RxHttp.requestTab.body_send = data or {}


  local httpsChars = nil
  if string.len(url) > 5 then
    httpsChars = string.sub(url,1, 5)
    -- RxLog('httpsChars='..tostring(httpsChars))
  end

  local status,header,body = 0, nil, nil
  if (httpsChars and httpsChars == 'https') then
    -- RxLog('RxHttp:Get -->>   h t t psss <<<<')
    status,header,body = ts.httpsGet(url, RxHttp.requestTab)
  else
    -- RxLog('RxHttp:Get -->>   h t t ppp <<<<')
    status,header,body = ts.httpGet(url, RxHttp.requestTab)
  end
  -- RxLog('status='..tostring(status))
  -- RxLog('header='..tostring(header))
  -- RxLog('body='..tostring(body))
  local result = body
  -- RxLog('RxHttp:Get 22222 isDealJson='..tostring(isDealJson))
  if isDealJson then
    RxLog('RxHttp:Get if isDealJson then ==>>>>'..tostring(isDealJson))
    result = RxHttpResponDeal(body)
  end
  return result
end

-- Post - http
function RxHttp:Post(url, data, isDealJson)
  if not url then return nil end

  if isDealJson == nil then
    isDealJson = true
  end

  local ts = require('ts')
  -- https://helpdoc.touchsprite.com/dev_docs/25/6.html
  -- 超时时间，单位为秒
  ts.setHttpsTimeOut(60)

  data = data or {}
  -- 这个是 android 的请求方式
  RxHttp.requestTab.body_send = data or {}

  local httpsChars = nil
  if string.len(url) > 5 then
    httpsChars = string.sub(url, 1, 5)
  end

  local status,header,body = 0, nil, nil
  if (httpsChars and httpsChars == 'https') then
    status,header,body = ts.httpsPost(url, RxHttp.requestTab)
  else
    status,header,body = ts.httpPost(url, RxHttp.requestTab)
  end

  -- RxLog('status='..tostring(status))
  -- RxLog('header='..tostring(header))
  -- RxLog('body='..tostring(body))
  local result = body
  if isDealJson then
    RxLog('RxHttp:Post if isDealJson then ==>>>>'..tostring(isDealJson))
    result = RxHttpResponDeal(body)
  end
  return result
end

function RxHttp:TestGet()
  local webdata = httpGet("https://m.qdongmall.com/api/mobile/islogin?mallType=1", 60)--获取百度首页网页数据
  RxLog('请求结果='..tostring(webdata))
  if webdata ~= "" then
    dialog(tostring(webdata))
  else
    dialog('请求-无数据')
  end
  luaExit()
  if true then return 0 end
end

function RxHttp:TestPostImg()
  local imgPath = userPath()..'/res/testsss.png'
  snapshot(imgPath, 0, 0, 200,200)
  function _readFileBase64(path)
    local f = io.open(path, 'rb')
    if f == nil then return nil end
    local bytes = f:read('*all')
    f:close()
    return bytes:base64_encode()
  end

  local imgString = _readFileBase64(imgPath)
  local url = ''
  local data = {img=imgString}
  local res = self:Post(url, data)
end

function RxHttp:tableJson(s)
  s = s or ''
  s = tostring(s)
  s = s:trim()
  -- RxLog('RxHttp:tableJson s='..tostring(s))

  local first = string.sub(s,1,1)
  -- RxLog('RxHttpResponDeal first='..tostring(first))
  if first == '{' then
    -- RxLog('RxHttpResponDeal first======{ 是对象 了}')
    local ts = require('ts')
    local cjson = ts.json
    local resTable = cjson.decode(s)
    return resTable
  end

  -- RxLog('RxHttpResponDeal first 处理结果不是正确的返回值')
  return s
end

return RxHttp